<!-- 饼图 -->
<template>
  <el-card>
    <template #header> 综合分析 </template>
    <div :id="id" :class="className" :style="{ height, width }"></div>
  </el-card>
</template>

<script setup lang="ts">
import * as echarts from "echarts";

const props = defineProps({
  id: {
    type: String,
    default: "pieChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "200px",
    required: true,
  },
  height: {
    type: String,
    default: "200px",
    required: true,
  },
  echartData: {
    type: Array,
    default: () => {
      return [];
    },
  },
});
const echartData = ref(props.echartData);
const options = {
  grid: {
    left: "2%",
    right: "2%",
    bottom: "10%",
    top: "0",
    containLabel: true,
  },
  tooltip: {
    trigger: "item",
    formatter: "{b}： {d}%",
  },
  legend: {
    top: "top",
    textStyle: {
      color: "#999",
    },
  },
  series: [
    {
      name: "本月巡检进度",
      type: "pie",
      center: ["50%", "55%"],
      radius: [30, 50],
      itemStyle: {
        borderRadius: 5,
        color: function (params: any) {
          //自定义颜色
          const colorList = ["#32bebd", "#67C23A", "#E6A23C", "#F56C6C"];
          return colorList[params.dataIndex];
        },
      },
      label: {
        fontSize: 12,
        formatter: function (data) {
          let str = "";
          console.log(data);
          str += data.data.name + ": " + data.percent + "%";
          return str;
        },
      },
      data: echartData.value,
    },
  ],
};

const chart = ref<any>("");

onMounted(() => {
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );

  chart.value.setOption(options);

  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});

onActivated(() => {
  if (chart.value) {
    chart.value.resize();
  }
});
</script>
